package com.cn.easy.export

import com.cn.easy.export.model.excel.ExcelColumn
import com.cn.easy.export.model.excel.ExcelTitle
import com.cn.easy.export.model.excel.ExportParam
import com.cn.easy.export.model.excel.SingleExcelColumn
import com.cn.easy.export.model.excel.SingleRowMultiExcelColumn
import com.cn.easy.export.service.excel.ExcelExportService
import org.apache.poi.ss.usermodel.BorderStyle
import org.apache.poi.ss.usermodel.HorizontalAlignment
import org.apache.poi.ss.usermodel.IndexedColors
import org.apache.poi.ss.usermodel.VerticalAlignment
import org.junit.jupiter.api.Test
import java.io.FileOutputStream

class TestWriteSingleRowMultiExcelData {
    @Test
    fun test() {
        val students = arrayListOf(
            Student(
                id = 1,
                name = "u1",
                age = 20,
                teacher = Teacher(id = 1, name = "t1", age = 30),
                cla = Cla(id = 1, name = "c2")
            ),
            Student(
                id = 2,
                name = "u2",
                age = 22,
                teacher = Teacher(id = 2, name = "t2", age = 30),
                cla = Cla(id = 1, name = "c1")
            ),
            Student(
                id = 3,
                name = "u3",
                age = 33,
                teacher = Teacher(id = 1, name = "t1", age = 30),
                cla = Cla(id = 1, name = null)
            ),
        )

        val headers = arrayListOf<ExcelColumn<Student>>(
            SingleExcelColumn(ExcelTitle("ID")) { it.id },
            SingleExcelColumn(ExcelTitle("姓名")) { it.name },
            SingleExcelColumn(ExcelTitle("年龄")) { it.age },
            SingleRowMultiExcelColumn(
                title = ExcelTitle("班主任"),
                children = listOf(
                    SingleExcelColumn(ExcelTitle("ID")) { it.teacher.id },
                    SingleExcelColumn(ExcelTitle("姓名")) { it.teacher.name },
                    SingleExcelColumn(ExcelTitle("年龄")) { it.teacher.age },
                )
            ),
            SingleRowMultiExcelColumn(
                title = ExcelTitle("班级"),
                children = listOf(
                    SingleExcelColumn(ExcelTitle("ID")) { it.cla?.id },
                    SingleExcelColumn(
                        title = ExcelTitle("班级名称"),
                        cellStyle = { workbook, row ->
                            if (row.cla?.name.isNullOrBlank()) {
                                val cellStyle = workbook.createCellStyle()
                                cellStyle.fillBackgroundColor = IndexedColors.RED.index
                                cellStyle
                            } else {
                                null
                            }

                        }
                    ) { it.cla?.name },
                )
            ),
        )

        val service = ExcelExportService(
            ExportParam(
                sheetName = "aaa",
                excelTitle = ExcelTitle(
                    "学生信息"
                ) { workbook ->
                    val cellStyle = workbook.createCellStyle()
                    //下边框
                    cellStyle.borderBottom = BorderStyle.THIN
                    //左边框
                    cellStyle.borderLeft = BorderStyle.THIN
                    //上边框
                    cellStyle.borderTop = BorderStyle.THIN
                    //右边框
                    cellStyle.borderRight = BorderStyle.THIN
                    cellStyle.alignment = HorizontalAlignment.CENTER

                    val font = workbook.createFont()
                    font.bold = true
                    font.fontName = "微软雅黑"
                    font.fontHeightInPoints = 24
                    cellStyle.setFont(font)

                    cellStyle
                },
                columns = headers,
                list = students,
                headerStyle = { workbook ->
                    val cellStyle = workbook.createCellStyle()
                    cellStyle.alignment = HorizontalAlignment.CENTER
                    cellStyle.verticalAlignment = VerticalAlignment.CENTER
                    val font = workbook.createFont()
                    font.bold = true
                    font.fontName = "微软雅黑"
                    font.fontHeightInPoints = 12
                    cellStyle.setFont(font)

                    cellStyle
                },
                cellStyle = { workbook ->
                    val cellStyle = workbook.createCellStyle()
                    cellStyle.alignment = HorizontalAlignment.CENTER
                    val font = workbook.createFont()
                    font.fontName = "微软雅黑"
                    font.fontHeightInPoints = 12
                    cellStyle.setFont(font)

                    cellStyle
                }
            )
        )

        service.export()
            .use {
                it.write(FileOutputStream("D:\\download\\test_write_single_row_multi_excel_data_1.xlsx"))
            }
    }

    private data class Teacher(
        val id: Int,
        val name: String,
        val age: Int,
    )


    private data class Student(
        val id: Int,
        val name: String,
        val age: Int,
        val teacher: Teacher,
        val cla: Cla?,
    )

    private data class Cla(
        val id: Int,
        val name: String?,
    )
}